#!/usr/bin/perl -wc

package RFCMail::ESMTP::Common;

use strict;
use RFCMail::Util::NetEnd;
use RFCMail::Common::Grammar qw(gen_id);


sub init {
    my $self = shift;
    $self->{in_fh} = \*STDIN
        if not defined($self->{in_fh});
    $self->{out_fh} = \*STDOUT
        if not defined($self->{out_fh});
    # Lines are delimited with <CRLF> unless filehandle is a tty.
    $self->{eol} = (-t $self->{in_fh}) ? $/ : "\015\012"
	if not defined($self->{eol});
    $self->{local_end} = RFCMail::Util::NetEnd->from_local($self->{in_fh})
	if not defined($self->{local_end});
    $self->{peer_end} = RFCMail::Util::NetEnd->from_peer($self->{in_fh})
	if not defined($self->{peer_end});
    $self->{domain} = $self->{local_end}->name
	if not defined($self->{domain});
    $self->{domain} = "mail.example.com"
	if not defined($self->{domain});
    $self->SUPER::init(@_);
    $self->{state} = {}
        if not defined($self->{state});
    $self->logmsg(d => "$0 instantiated ".ref($self)." for ".$self->{domain});
}


sub session {
    my $self = shift;
    $self->{state}->{Session_ID} = gen_id();
    return ($self->SUPER::session(@_));
}


sub logmsg {
    my $self = shift;
    my $level = shift;
    my @normal_lines = map { split(/\n+/) } @_;
    $self->SUPER::logmsg($level, @normal_lines);
}


sub cleanup {
    my $self = shift;
    $self->SUPER::cleanup(@_);
    $self->logmsg(d => ref($self)." object for ".$self->{domain}." expiring");
}



1;
